package com.neusoft.sysdata.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.qmdata.entity.*;
import com.neusoft.qmdata.service.*;
import io.netty.util.internal.StringUtil;
import lombok.SneakyThrows;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.YouBianCodeUtil;
import org.jeecg.common.util.oConvertUtils;
import com.neusoft.sysdata.entity.*;
import com.neusoft.sysdata.service.*;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 同步物流平台数据job任务
 *
 * @Author zhengdc
 */
@Slf4j
public class LmsSynDataJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    @Autowired
    private IMdCompanyService mdCompanyService;

    @Autowired
    private IMdDeptService mdDeptService;

    @Autowired
    private ISysSynDataService sysSynDataService;

    @Autowired
    private ISmRoleService smRoleService;

    @Autowired
    private ISmUserService smUserService;

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserDepartService sysUserDepartService;

    @Autowired
    private IVhVehicleService vhVehicleService;

    @Autowired
    private IVhVehicleTypeService vhVehicleTypeService;

    @Autowired
    private IRmFacilityService rmFacilityService;

    @Autowired
    private IRmFacilityTypeService rmFacilityTypeService;

    @Autowired
    private IRmSparepartService rmSparepartService;

    @Autowired
    private IRmSparepartTypeService rmSparepartTypeService;

    @Autowired
    private IQmVehicleService qmVehicleService;

    @Autowired
    public IQmVehicleTypeService qmVehicleTypeService;

    @Autowired
    public IQmFacilityService qmFacilityService;

    @Autowired
    public IQmFacilityTypeService qmFacilityTypeService;

    @Autowired
    public IQmSparepartService qmSparepartService;

    @Autowired
    public IQmSparepartTypeService qmSparepartTypeService;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info(String.format("时间：" + DateUtils.now() + "，参数: %s ", this.parameter));
        if (this.parameter != null) {
            synLmsData(this.parameter);
        } else {
            synLmsData("1");
            Thread.sleep(60 * 1000);
            synLmsData("2");
            Thread.sleep(60 * 1000);
            synLmsData("3");
            Thread.sleep(60 * 1000);
            synLmsData("4");
            Thread.sleep(60 * 1000);
            synLmsData("5");
            Thread.sleep(60 * 1000);
            synLmsData("6");
            Thread.sleep(60 * 1000);
            synLmsData("7");
        }
    }

    private boolean synLmsData(String type) {
        try {
            if (type.equals("1")) { //同步物流平台公司信息到当前平台的部门中
                long start = System.currentTimeMillis();
                List<MdCompany> mdCompanyList = mdCompanyService.selectCompanyInfo("-1");
                log.info("===================同步物流平台公司信息任务开始===================");
                for (MdCompany company : mdCompanyList) {
                    SysDepart sysDepart = new SysDepart();
                    sysDepart.setDepartName(company.getCompanyName());
                    int count = sysDepartService.count(new QueryWrapper<SysDepart>().lambda().eq(SysDepart::getDepartName, company.getCompanyName()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        sysDepart.setId(s);
                        sysDepart.setDepartNameEn(company.getShortCode());
                        //sysDepart.setAddress(company.getProvince() + company.getCity() + company.getCounty() + company.getArea());
                        //sysDepart.setDescription(company.getOrganization());
                        sysDepart.setOrgCategory("1");
                        // 获取父级ID
                        String parentId = sysDepart.getParentId();
                        String[] codeArray = generateOrgCode(parentId);
                        sysDepart.setOrgCode(codeArray[0]);
                        String orgType = codeArray[1];
                        sysDepart.setOrgType(String.valueOf(orgType));
                        sysDepart.setDepartOrder(0);
                        sysDepart.setDataSource("2");
                        sysDepart.setCreateBy("admin");
                        sysDepart.setCreateTime(new Date());
                        sysDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());

                        sysDepartService.save(sysDepart);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(company.getCompanyCode());
                        sysSynData.setRowName(company.getCompanyName());
                        sysSynData.setCodeId(company.getRowId().toString());
                        sysSynData.setParentCode(company.getParentCompany().toString());
                        sysSynData.setParentId(parentId);
                        sysSynData.setShortCode(company.getShortCode());
                        sysSynData.setShortName(company.getShortName());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(1);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);
                    } else {
                        sysDepart = sysDepartService.getOne(new QueryWrapper<SysDepart>().lambda().eq(SysDepart::getDepartName, company.getCompanyName()), false);
                    }
                    List<MdCompany> childCompanyList = mdCompanyService.selectCompanyInfo(company.getCompanyCode());
                    for (MdCompany childCompany : childCompanyList) {
                        SysDepart sysChildDepart = new SysDepart();
                        sysChildDepart.setDepartName(childCompany.getCompanyName());
                        int childCount = sysDepartService.count(new QueryWrapper<SysDepart>().lambda().eq(SysDepart::getDepartName, childCompany.getCompanyName()));
                        if (childCount == 0) {
                            String s = UUID.randomUUID().toString().replace("-", "");
                            sysChildDepart.setId(s);
                            sysChildDepart.setParentId(sysDepart.getId());
                            sysChildDepart.setDepartNameEn(childCompany.getShortCode());
                            //sysChildDepart.setAddress(childCompany.getProvince() + childCompany.getCity() + childCompany.getCounty() + childCompany.getArea());
                            //sysChildDepart.setDescription(childCompany.getOrganization());
                            sysChildDepart.setOrgCategory("1");
                            // 获取父级ID
                            String parentId = sysDepart.getId();
                            String[] codeChildArray = generateOrgCode(parentId);
                            sysChildDepart.setOrgCode(codeChildArray[0]);
                            String orgChildType = codeChildArray[1];
                            sysChildDepart.setOrgType(String.valueOf(orgChildType));
                            sysChildDepart.setDepartOrder(0);
                            sysChildDepart.setDataSource("2");
                            sysChildDepart.setCreateBy("admin");
                            sysChildDepart.setCreateTime(new Date());
                            sysChildDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                            sysDepartService.save(sysChildDepart);

                            SysSynData sysSynData = new SysSynData();
                            String rid = UUID.randomUUID().toString().replace("-", "");
                            sysSynData.setRowId(rid);
                            sysSynData.setRowCode(childCompany.getCompanyCode());
                            sysSynData.setRowName(childCompany.getCompanyName());
                            sysSynData.setCodeId(childCompany.getRowId().toString());
                            sysSynData.setParentCode(childCompany.getParentCompany().toString());
                            sysSynData.setParentId(parentId);
                            sysSynData.setShortCode(childCompany.getShortCode());
                            sysSynData.setShortName(childCompany.getShortName());
                            sysSynData.setNewRid(s);
                            sysSynData.setSysType(1);
                            sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                            sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                            sysSynDataService.save(sysSynData);
                        }
                    }
                }
                log.info("===================同步物流平台公司信息任务结束===================");
                long end = System.currentTimeMillis();
                long times = end - start;
                log.info("总耗时" + times + "毫秒");
            } else if (type.equals("2")) { //同步物流平台部门信息到当前平台的部门中
                long start = System.currentTimeMillis();
                List<SysSynData> sysSynDatas = sysSynDataService.selectSynDataInfo("1");
                log.info("===================同步物流平台部门信息任务开始===================");
                for (SysSynData depart : sysSynDatas) {
                    if (!oConvertUtils.isNumericzidai(depart.getRowCode())) {
                        continue;
                    }
                    List<MdDept> mdDeptList = mdDeptService.selectComDeptInfo(depart.getRowCode(), "-1");
                    for (MdDept dept : mdDeptList) {
                        if (dept.getStatus().equals("1")) {
                            continue;
                        }
                        SysDepart sysDepart = new SysDepart();
                        sysDepart.setDepartName(dept.getDeptName());
                        int count = sysDepartService.count(new QueryWrapper<SysDepart>().lambda().eq(SysDepart::getDepartName, dept.getDeptName()));
                        if (count == 0) {
                            String s = UUID.randomUUID().toString().replace("-", "");
                            sysDepart.setId(s);
                            sysDepart.setParentId(depart.getNewRid());
                            sysDepart.setDepartNameEn(dept.getShortCode());
                            sysDepart.setOrgCategory("2");
                            // 获取父级ID
                            String parentId = depart.getNewRid();
                            String[] codeArray = generateOrgCode(parentId);
                            sysDepart.setOrgCode(codeArray[0]);
                            String orgType = codeArray[1];
                            sysDepart.setOrgType(String.valueOf(orgType));
                            sysDepart.setDepartOrder(0);
                            sysDepart.setDataSource("2");
                            sysDepart.setCreateBy("admin");
                            sysDepart.setCreateTime(new Date());
                            sysDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());

                            sysDepartService.save(sysDepart);

                            SysSynData sysSynData = new SysSynData();
                            String rid = UUID.randomUUID().toString().replace("-", "");
                            sysSynData.setRowId(rid);
                            sysSynData.setRowCode(dept.getDeptCode());
                            sysSynData.setRowName(dept.getDeptName());
                            sysSynData.setCodeId(dept.getRowId().toString());
                            sysSynData.setParentCode(dept.getCompanyId().toString());
                            sysSynData.setParentId(parentId);
                            sysSynData.setShortCode(dept.getShortCode());
                            sysSynData.setShortName(dept.getShortName());
                            sysSynData.setNewRid(s);
                            sysSynData.setSysType(2);
                            sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                            sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                            sysSynDataService.save(sysSynData);
                        } else {
                            sysDepart = sysDepartService.getOne(new QueryWrapper<SysDepart>().lambda().eq(SysDepart::getDepartName, dept.getDeptName()), false);
                        }
                        List<MdDept> childDeptList = mdDeptService.selectComDeptInfo(depart.getRowCode(), dept.getRowId().toString());
                        for (MdDept childDept : childDeptList) {
                            SysDepart sysChildDepart = new SysDepart();
                            sysChildDepart.setDepartName(childDept.getDeptName());
                            int childCount = sysDepartService.count(new QueryWrapper<SysDepart>().lambda().eq(SysDepart::getDepartName, childDept.getDeptName()));
                            if (childCount == 0) {
                                String s = UUID.randomUUID().toString().replace("-", "");
                                sysChildDepart.setId(s);
                                sysChildDepart.setParentId(sysDepart.getId());
                                sysChildDepart.setDepartNameEn(childDept.getShortCode());
                                sysChildDepart.setOrgCategory("2");
                                // 获取父级ID
                                String parentId = sysDepart.getId();
                                String[] codeChildArray = generateOrgCode(parentId);
                                sysChildDepart.setOrgCode(codeChildArray[0]);
                                String orgChildType = codeChildArray[1];
                                sysChildDepart.setOrgType(String.valueOf(orgChildType));
                                sysChildDepart.setDepartOrder(0);
                                sysChildDepart.setDataSource("2");
                                sysChildDepart.setCreateBy("admin");
                                sysChildDepart.setCreateTime(new Date());
                                sysChildDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                                sysDepartService.save(sysChildDepart);

                                SysSynData sysSynData = new SysSynData();
                                String rid = UUID.randomUUID().toString().replace("-", "");
                                sysSynData.setRowId(rid);
                                sysSynData.setRowCode(childDept.getDeptCode());
                                sysSynData.setRowName(childDept.getDeptName());
                                sysSynData.setCodeId(childDept.getRowId().toString());
                                sysSynData.setParentCode(childDept.getCompanyId().toString());
                                sysSynData.setParentId(parentId);
                                sysSynData.setShortCode(childDept.getShortCode());
                                sysSynData.setShortName(childDept.getShortName());
                                sysSynData.setNewRid(s);
                                sysSynData.setSysType(2);
                                sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                                sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                                sysSynDataService.save(sysSynData);
                            } else {
                                sysChildDepart = sysDepartService.getOne(new QueryWrapper<SysDepart>().lambda().eq(SysDepart::getDepartName, childDept.getDeptName()), false);
                            }

                            List<MdDept> childChildDeptList = mdDeptService.selectPDeptInfo(childDept.getRowId().toString());
                            for (MdDept childChildDept : childChildDeptList) {
                                SysDepart sysChildChildDepart = new SysDepart();
                                sysChildChildDepart.setDepartName(childChildDept.getDeptName());
                                int childChildCount = sysDepartService.count(new QueryWrapper<SysDepart>().lambda().eq(SysDepart::getDepartName, childChildDept.getDeptName()));
                                if (childChildCount == 0) {
                                    String s = UUID.randomUUID().toString().replace("-", "");
                                    sysChildChildDepart.setId(s);
                                    sysChildChildDepart.setParentId(sysChildDepart.getId());
                                    sysChildChildDepart.setDepartNameEn(childChildDept.getShortCode());
                                    sysChildChildDepart.setOrgCategory("2");
                                    // 获取父级ID
                                    String parentId = sysChildDepart.getId();
                                    String[] codeChildArray = generateOrgCode(parentId);
                                    sysChildChildDepart.setOrgCode(codeChildArray[0]);
                                    String orgChildType = codeChildArray[1];
                                    sysChildChildDepart.setOrgType(String.valueOf(orgChildType));
                                    sysChildChildDepart.setDepartOrder(0);
                                    sysChildChildDepart.setDataSource("2");
                                    sysChildChildDepart.setCreateBy("admin");
                                    sysChildChildDepart.setCreateTime(new Date());
                                    sysChildChildDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                                    sysDepartService.save(sysChildChildDepart);

                                    SysSynData sysSynData = new SysSynData();
                                    String rid = UUID.randomUUID().toString().replace("-", "");
                                    sysSynData.setRowId(rid);
                                    sysSynData.setRowCode(childChildDept.getDeptCode());
                                    sysSynData.setRowName(childChildDept.getDeptName());
                                    sysSynData.setCodeId(childChildDept.getRowId().toString());
                                    sysSynData.setParentCode(childChildDept.getCompanyId().toString());
                                    sysSynData.setParentId(parentId);
                                    sysSynData.setShortCode(childChildDept.getShortCode());
                                    sysSynData.setShortName(childChildDept.getShortName());
                                    sysSynData.setNewRid(s);
                                    sysSynData.setSysType(2);
                                    sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                                    sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                                    sysSynDataService.save(sysSynData);
                                }
                            }
                        }
                    }
                }
                log.info("===================同步物流平台部门信息任务结束===================");
                long end = System.currentTimeMillis();
                long times = end - start;
                log.info("总耗时" + times + "毫秒");
            } else if (type.equals("3")) { //同步物流平台用户信息到当前平台的用户信息中
                long start = System.currentTimeMillis();
                List<SmUser> userList = smUserService.list();
                log.info("===================同步物流平台用户信息任务开始===================");
                for (SmUser user : userList) {
                    if (user.getDeletedFlag().equals("1")) {
                        continue;
                    }
                    SysUser sysUser = new SysUser();
                    sysUser.setUsername(user.getUserCode());
                    int count = sysUserService.count(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername, user.getUserCode()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        sysUser.setId(s);
                        sysUser.setRealname(user.getUserName());
                        sysUser.setPassword(user.getPassword());
                        SysSynData companyDepart = sysSynDataService.selectSynDataOneInfo(user.getCompanyId().toString(), "1");
                        if (companyDepart != null) {
                            SysDepart sysDepart = sysDepartService.getById(companyDepart.getNewRid());
                            if (sysDepart != null) {
                                sysUser.setOrgCode(sysDepart.getOrgCode());
                            }
                        } else {
                            companyDepart = sysSynDataService.selectSynDataOneInfo(user.getCompanyId().toString(), "2");
                            if (companyDepart != null) {
                                SysDepart sysDepart = sysDepartService.getById(companyDepart.getNewRid());
                                if (sysDepart != null) {
                                    sysUser.setOrgCode(sysDepart.getOrgCode());
                                }
                            }
                        }
                        sysUser.setStatus(CommonConstant.OPER_STATUS_1);
                        sysUser.setEmail(user.getEmail());
                        sysUser.setPhone(user.getMobile());
                        sysUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysUser.setActivitiSync(CommonConstant.ACT_SYNC_1);
                        sysUser.setDataSource("2");
                        sysUser.setCreateBy("admin");
                        sysUser.setCreateTime(new Date());

                        sysUserService.save(sysUser);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(user.getUserCode());
                        sysSynData.setRowName(user.getUserName());
                        sysSynData.setCodeId(user.getRowId().toString());
                        sysSynData.setParentCode(user.getCompanyId().toString());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(3);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);

                        if (companyDepart != null) {
                            SysUserDepart sysUserDepart = new SysUserDepart(null, sysUser.getId(), companyDepart.getNewRid());
                            QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<SysUserDepart>();
                            queryWrapper.eq("dep_id", companyDepart.getNewRid()).eq("user_id", sysUser.getId());
                            SysUserDepart one = sysUserDepartService.getOne(queryWrapper);
                            if (one == null) {
                                sysUserDepartService.save(sysUserDepart);
                            }
                        }
                    }
                }
                log.info("===================同步物流平台用户信息任务结束===================");
                long end = System.currentTimeMillis();
                long times = end - start;
                log.info("总耗时" + times + "毫秒");
            } else if (type.equals("4")) { //同步物流平台角色信息到当前平台的用户信息中
                long start = System.currentTimeMillis();
                List<SmRole> roleList = smRoleService.list();
                log.info("===================同步物流平台角色信息任务开始===================");
                for (SmRole role : roleList) {
                    if (role.getDeletedFlag().equals("1")) {
                        continue;
                    }
                    SysRole sysRole = new SysRole();
                    sysRole.setRoleName(role.getRoleName());
                    int count = sysRoleService.count(new QueryWrapper<SysRole>().lambda().eq(SysRole::getRoleName, role.getRoleName()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        sysRole.setId(s);
                        sysRole.setRoleCode(role.getRoleCode());
                        sysRole.setDataSource("2");
                        sysRole.setCreateBy("admin");
                        sysRole.setCreateTime(new Date());

                        sysRoleService.save(sysRole);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(role.getRoleCode());
                        sysSynData.setRowName(role.getRoleName());
                        sysSynData.setCodeId(role.getRowId().toString());
                        sysSynData.setParentCode(role.getCompanyId().toString());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(4);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);
                    }
                }
                log.info("===================同步物流平台角色信息任务结束===================");
                long end = System.currentTimeMillis();
                long times = end - start;
                log.info("总耗时" + times + "毫秒");
            } else if (type.equals("5")) { //同步物流平台车辆信息
                long start = System.currentTimeMillis();
                List<VhVehicleType> vehicleTypeList = vhVehicleTypeService.list();
                List<VhVehicle> vehicleList = vhVehicleService.list();
                log.info("===================同步物流平台车辆信息任务开始===================");
                for (VhVehicleType vhVehicleType : vehicleTypeList) {
                    QmVehicleType qmVehicleType = new QmVehicleType();
                    qmVehicleType.setTypeCode(vhVehicleType.getTypeCode());
                    int count = qmVehicleTypeService.count(new QueryWrapper<QmVehicleType>().lambda().eq(QmVehicleType::getTypeCode, vhVehicleType.getTypeCode()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        qmVehicleType.setId(s);
                        qmVehicleType.setTypeName(vhVehicleType.getTypeName());
                        qmVehicleType.setAvgOilConsumption(vhVehicleType.getAvgOilConsumption());
                        qmVehicleType.setCarryingCapacity(vhVehicleType.getCarryingCapacity());
                        qmVehicleType.setManufactory(vhVehicleType.getManufactory());
                        qmVehicleType.setQuantity(BigDecimal.valueOf(vhVehicleType.getQuantity()));
                        if (vhVehicleType.getSeating() != null) {
                            qmVehicleType.setSeating(BigDecimal.valueOf(vhVehicleType.getSeating()));
                        }
                        qmVehicleType.setSpecification(vhVehicleType.getSpecification());
                        qmVehicleType.setIsDelivery(Integer.valueOf(vhVehicleType.getIsdelivery() == "Y" ? "1" : "0"));
                        qmVehicleType.setDataSource("2");
                        qmVehicleType.setCreateBy("admin");
                        qmVehicleType.setCreateTime(new Date());
                        qmVehicleType.setStatus(CommonConstant.OPER_STATUS_1);

                        qmVehicleTypeService.save(qmVehicleType);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(vhVehicleType.getTypeCode());
                        sysSynData.setRowName(vhVehicleType.getTypeName());
                        sysSynData.setCodeId(vhVehicleType.getRowId().toString());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(5);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);
                    }
                }
                //Thread.sleep(30 * 1000);
                for (VhVehicle vhVehicle : vehicleList) {
                    QmVehicle qmVehicle = new QmVehicle();
                    qmVehicle.setVehicleSign(vhVehicle.getVehicleSign());
                    int count = qmVehicleService.count(new QueryWrapper<QmVehicle>().lambda().eq(QmVehicle::getVehicleSign, vhVehicle.getVehicleSign()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        qmVehicle.setId(s);
                        SysSynData typeVehicle = sysSynDataService.selectSynRowOneInfo(vhVehicle.getVehicleType().toString(), "5");
                        if (typeVehicle != null) {
                            QmVehicleType qmVehicleType = qmVehicleTypeService.getById(typeVehicle.getNewRid());
                            if (qmVehicleType != null) {
                                qmVehicle.setTypeId(qmVehicleType.getId());
                            }
                        }
                        SysSynData typeDepart = sysSynDataService.selectSynRowOneInfo(vhVehicle.getDeptId().toString(), "2");
                        if (typeDepart != null) {
                            SysDepart sysDepart = sysDepartService.getById(typeDepart.getNewRid());
                            if (sysDepart != null) {
                                qmVehicle.setDepartId(sysDepart.getId());
                            }
                        }
                        qmVehicle.setDataSource("2");
                        qmVehicle.setCreateBy("admin");
                        qmVehicle.setCreateTime(new Date());
                        qmVehicle.setStatus(CommonConstant.OPER_STATUS_1);

                        qmVehicleService.save(qmVehicle);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(vhVehicle.getVehicleSign());
                        sysSynData.setCodeId(vhVehicle.getRowId().toString());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(6);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);
                    }
                }
                log.info("===================同步物流平台车辆信息任务结束===================");
                long end = System.currentTimeMillis();
                long times = end - start;
                log.info("总耗时" + times + "毫秒");
            } else if (type.equals("6")) { //同步物流平台设备信息
                long start = System.currentTimeMillis();
                List<RmFacilityType> facilityTypeList = rmFacilityTypeService.list();
                List<RmFacility> facilityList = rmFacilityService.list();
                log.info("===================同步物流平台设备信息任务开始===================");
                for (RmFacilityType rmFacilityType : facilityTypeList) {
                    QmFacilityType facilityType = new QmFacilityType();
                    facilityType.setTypeCode(rmFacilityType.getTypeCode());
                    int count = qmFacilityTypeService.count(new QueryWrapper<QmFacilityType>().lambda().eq(QmFacilityType::getTypeCode, rmFacilityType.getTypeCode()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        facilityType.setId(s);
                        SysSynData typeVehicle = sysSynDataService.selectSynRowOneInfo(rmFacilityType.getParTypeId().toString(), "7");
                        if (typeVehicle != null) {
                            QmFacilityType qmFacilityType = qmFacilityTypeService.getById(typeVehicle.getNewRid());
                            if (qmFacilityType != null) {
                                facilityType.setParentId(qmFacilityType.getId());
                            }
                        }
                        facilityType.setTypeName(rmFacilityType.getTypeName());
                        facilityType.setDataSource("2");
                        facilityType.setCreateBy("admin");
                        facilityType.setCreateTime(new Date());
                        facilityType.setStatus(CommonConstant.OPER_STATUS_1);

                        qmFacilityTypeService.save(facilityType);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(rmFacilityType.getTypeCode());
                        sysSynData.setRowName(rmFacilityType.getTypeName());
                        sysSynData.setCodeId(rmFacilityType.getRowId().toString());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(7);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);
                    }
                }
                //Thread.sleep(30 * 1000);
                for (RmFacility rmFacility : facilityList) {
                    QmFacility facility = new QmFacility();
                    facility.setFacilityCode(rmFacility.getAssetCode());
                    int count = qmFacilityService.count(new QueryWrapper<QmFacility>().lambda().eq(QmFacility::getFacilityCode, rmFacility.getAssetCode()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        facility.setId(s);
                        SysSynData typeVehicle = sysSynDataService.selectSynRowOneInfo(rmFacility.getTypeId().toString(), "7");
                        if (typeVehicle != null) {
                            QmFacilityType qmFacilityType = qmFacilityTypeService.getById(typeVehicle.getNewRid());
                            if (qmFacilityType != null) {
                                facility.setTypeId(qmFacilityType.getId());
                            }
                        }
                        SysSynData typeDepart = sysSynDataService.selectSynRowOneInfo(rmFacility.getDeptId().toString(), "2");
                        if (typeDepart != null) {
                            SysDepart sysDepart = sysDepartService.getById(typeDepart.getNewRid());
                            if (sysDepart != null) {
                                facility.setDepartId(sysDepart.getId());
                            }
                        }
                        SysSynData parentFacility = sysSynDataService.selectSynRowOneInfo(rmFacility.getPAssetId().toString(), "8");
                        if (parentFacility != null) {
                            QmFacility qmFacility = qmFacilityService.getById(parentFacility.getNewRid());
                            if (qmFacility != null) {
                                facility.setParentId(qmFacility.getId());
                            }
                        }
                        facility.setFacilityName(rmFacility.getAssetName());
                        facility.setFacilityNo(rmFacility.getAssetNo());
                        facility.setBaseValue(rmFacility.getUsedValue());
                        facility.setBeginUseDate(rmFacility.getBeginUseDate());
                        facility.setLeaveFactoryDate(rmFacility.getLeaveFactoryDate());
                        facility.setMeasureUnit(rmFacility.getMeasureUnit());
                        facility.setWarrantiesDate(rmFacility.getPurchaseDate());
                        facility.setWeight(rmFacility.getWeight());
                        facility.setDataSource("2");
                        facility.setCreateBy("admin");
                        facility.setCreateTime(new Date());
                        facility.setStatus(CommonConstant.OPER_STATUS_1);

                        qmFacilityService.save(facility);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(rmFacility.getAssetCode());
                        sysSynData.setRowName(rmFacility.getAssetName());
                        sysSynData.setParentCode(rmFacility.getPAssetId().toString());
                        sysSynData.setCodeId(rmFacility.getRowId().toString());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(8);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);
                    }
                }
                log.info("===================同步物流平台设备信息任务结束===================");
                long end = System.currentTimeMillis();
                long times = end - start;
                log.info("总耗时" + times + "毫秒");
            } else if (type.equals("7")) { //同步物流平台商品信息
                long start = System.currentTimeMillis();
                List<RmSparepartType> sparepartTypeList = rmSparepartTypeService.list();
                List<RmSparepart> sparepartList = rmSparepartService.list();
                log.info("===================同步物流平台商品信息任务开始===================");
                for (RmSparepartType rmSparepartType : sparepartTypeList) {
                    QmSparepartType sparepartType = new QmSparepartType();
                    sparepartType.setTypeCode(rmSparepartType.getTypeCode());
                    int count = qmSparepartTypeService.count(new QueryWrapper<QmSparepartType>().lambda().eq(QmSparepartType::getTypeCode, rmSparepartType.getTypeCode()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        sparepartType.setId(s);
                        SysSynData typeVehicle = sysSynDataService.selectSynRowOneInfo(rmSparepartType.getParTypeId().toString(), "9");
                        if (typeVehicle != null) {
                            QmSparepartType qmSparepartType = qmSparepartTypeService.getById(typeVehicle.getNewRid());
                            if (qmSparepartType != null) {
                                sparepartType.setParentId(qmSparepartType.getId());
                            }
                        }
                        sparepartType.setTypeName(rmSparepartType.getTypeName());
                        sparepartType.setDataSource("2");
                        sparepartType.setCreateBy("admin");
                        sparepartType.setCreateTime(new Date());
                        sparepartType.setStatus(CommonConstant.OPER_STATUS_1);

                        qmSparepartTypeService.save(sparepartType);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(rmSparepartType.getTypeCode());
                        sysSynData.setRowName(rmSparepartType.getTypeName());
                        sysSynData.setCodeId(rmSparepartType.getRowId().toString());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(9);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);
                    }
                }
                //Thread.sleep(30 * 1000);
                for (RmSparepart rmSparepart : sparepartList) {
                    QmSparepart sparepart = new QmSparepart();
                    sparepart.setSparepartCode(rmSparepart.getSparepartCode());
                    int count = qmSparepartService.count(new QueryWrapper<QmSparepart>().lambda().eq(QmSparepart::getSparepartCode, rmSparepart.getSparepartCode()));
                    if (count == 0) {
                        String s = UUID.randomUUID().toString().replace("-", "");
                        sparepart.setId(s);
                        SysSynData typeVehicle = sysSynDataService.selectSynRowOneInfo(rmSparepart.getTypeId().toString(), "9");
                        if (typeVehicle != null) {
                            QmSparepartType qmSparepartType = qmSparepartTypeService.getById(typeVehicle.getNewRid());
                            if (qmSparepartType != null) {
                                sparepart.setTypeId(qmSparepartType.getId());
                            }
                        }
                        sparepart.setSparepartName(rmSparepart.getSparepartName());
                        sparepart.setShortCode(rmSparepart.getShortCode());
                        sparepart.setEnglishName(rmSparepart.getEnglishName());
                        sparepart.setSparepartType(rmSparepart.getSparepartType());
                        sparepart.setUnit(rmSparepart.getUnit());
                        sparepart.setRemark(rmSparepart.getRemark());
                        sparepart.setDataSource("2");
                        sparepart.setCreateBy("admin");
                        sparepart.setCreateTime(new Date());
                        sparepart.setStatus(CommonConstant.OPER_STATUS_1);

                        qmSparepartService.save(sparepart);

                        SysSynData sysSynData = new SysSynData();
                        String rid = UUID.randomUUID().toString().replace("-", "");
                        sysSynData.setRowId(rid);
                        sysSynData.setRowCode(rmSparepart.getSparepartCode());
                        sysSynData.setRowName(rmSparepart.getSparepartName());
                        sysSynData.setCodeId(rmSparepart.getRowId().toString());
                        sysSynData.setNewRid(s);
                        sysSynData.setSysType(10);
                        sysSynData.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                        sysSynData.setStatus(CommonConstant.OPER_STATUS_1.toString());
                        sysSynDataService.save(sysSynData);
                    }
                }
                log.info("===================同步物流平台商品信息任务结束===================");
                long end = System.currentTimeMillis();
                long times = end - start;
                log.info("总耗时" + times + "毫秒");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * saveDepartData 的调用方法,生成部门编码和部门类型
     *
     * @param parentId
     * @return
     */
    private String[] generateOrgCode(String parentId) {
        //update-begin--Author:Steve  Date:20190201 for：组织机构添加数据代码调整
        LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
        LambdaQueryWrapper<SysDepart> query1 = new LambdaQueryWrapper<SysDepart>();
        String[] strArray = new String[2];
        // 创建一个List集合,存储查询返回的所有SysDepart对象
        List<SysDepart> departList = new ArrayList<>();
        // 定义新编码字符串
        String newOrgCode = "";
        // 定义旧编码字符串
        String oldOrgCode = "";
        // 定义部门类型
        String orgType = "";
        // 如果是最高级,则查询出同级的org_code, 调用工具类生成编码并返回
        if (StringUtil.isNullOrEmpty(parentId)) {
            // 线判断数据库中的表是否为空,空则直接返回初始编码
            query1.eq(SysDepart::getParentId, "").or().isNull(SysDepart::getParentId);
            query1.orderByDesc(SysDepart::getOrgCode);
            departList = sysDepartService.list(query1);
            if (departList == null || departList.size() == 0) {
                strArray[0] = YouBianCodeUtil.getNextYouBianCode(null);
                strArray[1] = "1";
                return strArray;
            } else {
                SysDepart depart = departList.get(0);
                oldOrgCode = depart.getOrgCode();
                orgType = depart.getOrgType();
                newOrgCode = YouBianCodeUtil.getNextYouBianCode(oldOrgCode);
            }
        } else { // 反之则查询出所有同级的部门,获取结果后有两种情况,有同级和没有同级
            // 封装查询同级的条件
            query.eq(SysDepart::getParentId, parentId);
            // 降序排序
            query.orderByDesc(SysDepart::getOrgCode);
            // 查询出同级部门的集合
            List<SysDepart> parentList = sysDepartService.list(query);
            // 查询出父级部门
            SysDepart depart = sysDepartService.getById(parentId);
            // 获取父级部门的Code
            String parentCode = depart.getOrgCode();
            // 根据父级部门类型算出当前部门的类型
            orgType = String.valueOf(Integer.valueOf(depart.getOrgType()) + 1);
            // 处理同级部门为null的情况
            if (parentList == null || parentList.size() == 0) {
                // 直接生成当前的部门编码并返回
                newOrgCode = YouBianCodeUtil.getSubYouBianCode(parentCode, null);
            } else { //处理有同级部门的情况
                // 获取同级部门的编码,利用工具类
                String subCode = parentList.get(0).getOrgCode();
                // 返回生成的当前部门编码
                newOrgCode = YouBianCodeUtil.getSubYouBianCode(parentCode, subCode);
            }
        }
        // 返回最终封装了部门编码和部门类型的数组
        strArray[0] = newOrgCode;
        strArray[1] = orgType;
        return strArray;
        //update-end--Author:Steve  Date:20190201 for：组织机构添加数据代码调整
    }
}
